<script>
import WebIM from "./common/initWeb";
import {config} from "@/common/WebIMConfig";

export default{
  name: "App",
   beforeMount(){
    const phone = localStorage.getItem("phone")
    const password = localStorage.getItem("password")

    if (password) {
      let options = {
        user: phone,
        pwd: password,
        appKey: config.appkey,
        success: function (res) {
          console.log("登录成功：" + res.access_token)
          localStorage.setItem("imToken", res.access_token)
        }
      };
      this.WebIm.conn.open(options);
    }
  },
  created() {
    const _this = this
    this.WebIm.conn.listen({
      onTextMessage: function (message) {
        // 如果是给当前聊天用户发消息，则添加到msgList
        if (_this.friendPhone == message.from) {
          _this.msgList.push({isMe: false, text: message.data, id: message.serverMsgId})
        }
        //存储消息到本地
        let localMsg = localStorage.getItem(message.to + "-" + message.from);
        let msgList = []
        if (localMsg) {
          msgList = JSON.parse(localMsg)
        }
        msgList.push({isMe: false, text: message.data, id: message.id})
        localStorage.setItem(message.to + "-" + message.from, JSON.stringify(msgList))
      }
    })
  }
};
</script>

<template>
  <view id="app">
    <router-view/>
  </view>

</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  max-width: 500px;
  margin: auto;
}
.content {
  background: #F7F8FA;
  padding: 10px;
  min-height: 100vh;
}
</style>
